unit clsDivisao;

interface

uses Dm, dbtables, biblio;

type
  TDivisao = class(TObject)
  private
    Div_codigo: string;
  public
    Div_descricao: string;
    function Gravar(p_Div_codigo: string): Byte;
    function Recuperar(p_Div_codigo: string): Byte;
    function Excluir(p_Div_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TDivisao.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Divisao where Div_codigo=:k_Div_codigo');
  tq.parambyname('k_Div_codigo').asstring := strzero(p_Div_codigo, 2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Divisao VALUES(:k_Div_codigo,:k_Div_descricao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Divisao SET Div_descricao=:k_Div_descricao WHERE Div_codigo=:k_Div_codigo');
  end;
  tq.parambyname('k_Div_codigo').asstring := strzero(p_Div_codigo, 2);
  tq.parambyname('k_Div_descricao').asstring := Div_descricao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TDivisao.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Divisao where Div_codigo=:k_Div_codigo');
  tq.Params[0].asstring := strzero(p_Div_codigo, 2);
  tq.open;
  Div_codigo := p_Div_codigo;
  if tq.eof then begin
    Div_descricao := '';
    Result := 0;
  end else begin
    Div_descricao := tq.fieldbyname('Div_descricao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TDivisao.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Divisao WHERE Div_codigo=:k_Div_codigo');
  tq.parambyname('k_Div_codigo').asstring := strzero(p_Div_codigo, 2);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TDivisao.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Divisao where Div_descricao>=:k_Div_descricao order by Div_descricao');
  p_q.parambyname('k_Div_descricao').asstring := p_nome;
end;

end.
